package com.example.mytestdemo.linearlayoutmanager.dragon

import android.graphics.Path
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityLinearinglayoutManagerRecycleviewDragonLayoutBinding
import com.example.mytestdemo.databinding.ActivityLinearinglayoutManagerRecycleviewItemLayoutBinding
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import java.util.*

/**
 *参考：https://blog.csdn.net/u011387817/article/details/80313184
 * https://blog.csdn.net/AliEnCheng/article/details/122541863
 */
class CustomRecycleDragonActivity :
    BaseViewBindActivity<ActivityLinearinglayoutManagerRecycleviewDragonLayoutBinding>() {
    lateinit var toast: Toast


    private lateinit var mView: View

    override fun getLayoutInflateId(): Int {
        return R.layout.activity_linearinglayout_manager_recycleview_dragon_layout
    }

    override fun initData() {

    }

    override fun initUI() {
        mView = mBind.recycleView
        toast = Toast.makeText(this, "aa", Toast.LENGTH_SHORT)

        initAdapter()

        lifecycleScope.launch (CoroutineName("A") + SupervisorJob()){
            Log.d(TAG, "initUI: ${coroutineContext}")
            launch {
                Log.d(TAG, "initUI new : ${coroutineContext}")
            }
        }
    }

    private fun initAdapter() {
        val mData = arrayListOf<String>()
        (0..100).forEach { mData.add("事项$it") }
        val adapter = MyAdapter(mData)
//        mBind.recycleView.layoutManager = PathLinearLayoutManager(this,getPath(),100)
        //自定义路径
        mBind.recycleView.layoutManager = PathLinearLayoutManager(this,getCustomPath(),100)
        mBind.recycleView.adapter = adapter
    }

    private fun getCustomPath(): Path {
        return mBind.drawPath.getPath()
    }

    private fun getPath(): Path {
        val path = Path()
        path.moveTo(50f, 50f)
        path.rLineTo(500f, 600f)
//        path.rLineTo(-300f, 100f)
//        path.rLineTo(300f, 100f)
//        path.rLineTo(-300f, 100f)
        return path
//        return mBind.drawPath.getPath()
    }

}

class MyAdapter(val mData: List<String>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {


    private lateinit var mBind: ActivityLinearinglayoutManagerRecycleviewItemLayoutBinding

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        mBind = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.activity_linearinglayout_manager_recycleview_item_layout,
            parent,
            false
        )
        return ViewHolder(mBind)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(mData[position], position)
    }

    override fun getItemCount(): Int {
        return mData.size
    }


    class ViewHolder(val mBind: ActivityLinearinglayoutManagerRecycleviewItemLayoutBinding) :
        RecyclerView.ViewHolder(mBind.root) {
        fun bindData(data: String, position: Int) {
            mBind.tv.text = data
            mBind.root.setOnClickListener {
                Toast.makeText(itemView.context, "点击了$position", Toast.LENGTH_SHORT).show()
            }
        }
    }
}